<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm"  :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车间编码" prop="workshopCode">
        <el-input
          v-model="queryParams.workshopCode"
          placeholder="请输入车间编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车间名称" prop="workshopName">
        <el-input
          v-model="queryParams.workshopName"
          placeholder="请输入车间名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:md:workshop:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:md:workshop:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:md:workshop:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workshopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车间编码" align="center" prop="workshopCode" >
        <template #default="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['mes:md:workshop:query']"
          >{{scope.row.workshopCode}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="车间名称" align="center" prop="workshopName" />
      <el-table-column label="面积" align="center" prop="area" />
      <el-table-column label="负责人" align="center" prop="charge" />
      <el-table-column label="是否启用" align="center" prop="enableFlag" >
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.enableFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:md:workshop:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:md:workshop:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车间对话框 -->
    <el-dialog :title="title" v-model="open" width="960px" append-to-body>
      <el-form ref="workshopForm" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="车间编码" prop="workshopCode">
              <el-input v-model="form.workshopCode" placeholder="请输入车间编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item  label-width="80">
                <el-switch v-model="autoGenFlag"
                    active-color="#13ce66"
                    active-text="自动生成"
                    @change="handleAutoGenChange(autoGenFlag)" v-if="optType != 'view'">               
                </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="车间名称" prop="workshopName">
              <el-input v-model="form.workshopName" placeholder="请输入车间名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="面积" prop="area">
              <el-input-number :min="0" :percision="2" :step="1" v-model="form.area" placeholder="请输入面积" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="负责人" prop="charge">
              <el-input v-model="form.charge" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="enableFlag">
              <el-radio-group v-model="form.enableFlag" disabled v-if="optType=='view'">
                <el-radio
                  v-for="dict in sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
              <el-radio-group v-model="form.enableFlag" v-else>
                <el-radio
                  v-for="dict in sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="cancel" v-if="optType =='view'">返回</el-button>
          <el-button type="primary" @click="submitForm" v-else>确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
    </template>
    </el-dialog>
  </div>
</template>

<script setup class="Workshop">
import { listWorkshop, getWorkshop, delWorkshop, addWorkshop, updateWorkshop } from "@/api/mes/md/workshop";
import {genCode} from "@/api/system/autocode/rule"

const { proxy } = getCurrentInstance();
const { sys_yes_no } = proxy.useDict("sys_yes_no");


const loading = ref(true);// 遮罩层
const ids = ref([]); // 选中数组
const showSearch = ref(true);// 显示搜索条件
const single = ref(true);// 非单个禁用
const multiple = ref(true);// 非多个禁用
const total = ref(0);// 总条数
const title = ref(""); // 弹出层标题
const open = ref(false);// 是否显示弹出层

const autoGenFlag = ref(false); 
const optType = ref(undefined); 
const workshopList = ref([]); //部门树选项

const data = reactive({
  form: {},// 表单参数
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    workshopCode: null,
    workshopName: null,
    area: null,
    charge: null,
    enableFlag: null,
  },
  // 表单校验
  rules: {
        workshopCode: [
          { required: true, message: "车间编码不能为空", trigger: "blur" }
        ],
        workshopName: [
          { required: true, message: "车间名称不能为空", trigger: "blur" }
        ],
        enableFlag: [
          { required: true, message: "是否启用不能为空", trigger: "blur" }
        ],
      }
});

const { queryParams, form, rules } = toRefs(data);//对象解构

 /** 查询车间列表 */
 function getList() {
      loading.value = true;
      listWorkshop(queryParams.value).then(response => {
        workshopList.value = response.rows;
        total.value = response.total;
        loading.value = false;
      });
    }
    // 取消按钮
  function cancel() {
      open.value = false;
      reset();
    }
    // 表单重置
function reset() {
    form.value = {
      workshopId: null,
      workshopCode: null,
      workshopName: null,
      area: null,
      charge: null,
      enableFlag: 'Y',
      remark: null,
      attr1: null,
      attr2: null,
      attr3: null,
      attr4: null,
      createBy: null,
      createTime: null,
      updateBy: null,
      updateTime: null
    };
    autoGenFlag.value = false;
    proxy.resetForm("workshopForm");
  }

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.workshopId)
  single.value = selection.length!==1
  multiple.value = !selection.length
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加车间";
  optType.value = "add";
}

// 查询明细按钮操作
function handleView(row){
  reset();
  const workshopId = row.workshopId || ids.value
  getWorkshop(workshopId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "查看车间";
    optType.value = "view";
  });
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const workshopId = row.workshopId || ids.value
  getWorkshop(workshopId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改车间";
    optType.value = "edit";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["workshopForm"].validate(valid => {
    if (valid) {
      if (form.value.workshopId != null) {
        updateWorkshop(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWorkshop(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const workshopIds = row.workshopId || ids.value;
  proxy.$modal.confirm('是否确认删除车间编号为"' + workshopIds + '"的数据项？').then(function() {
    return delWorkshop(workshopIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  this.download('md/workshop/export', {
    ...queryParams.value
  }, `workshop_${new Date().getTime()}.xlsx`)
}
//自动生成物料编码
function handleAutoGenChange(autoGenFlag){

  if(autoGenFlag){
    genCode('WORKSHOP_CODE').then(response =>{
      form.value.workshopCode = response;
    });
  }else{
    form.value.workshopCode = null;
  }
  
}

getList();

</script>
